package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Balance;
import com.example.demo.entity.BalanceArea;
import com.example.demo.entity.Post;
import com.example.demo.entity.PostArea;
import com.example.demo.mapper.PostAreaMapper;
import com.example.demo.service.PostAreaService;
import com.example.demo.service.PostService;
import javafx.geometry.Pos;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Atanycosts
 * @description 针对表【post_area】的数据库操作Service实现
 * @createDate 2023-05-27 18:12:10
 */
@Service
public class PostAreaServiceImpl extends ServiceImpl<PostAreaMapper, PostArea>
        implements PostAreaService {

    @Autowired
    private PostAreaMapper postAreaMapper;

    @Autowired
    private PostService postService;


    @Override
    public List<Post> getAllPostArea(String province) {
        //在方案区域表中 只查询出BalanceID
        List<Integer> postIds = new LambdaQueryChainWrapper<PostArea>(postAreaMapper)
                .eq(PostArea::getProvince, province)
                .list()
                .stream()
                .map(PostArea::getPostId)
                .collect(Collectors.toList());

        if (postIds.size() == 0) {
            throw new RuntimeException("此地区帖子为空");
        }

        List<Post> posts = new ArrayList<>();
        postIds.stream().map((item) -> {
            posts.add(postService.getById(item));
            return posts;
        }).collect(Collectors.toList());

        return posts;
    }
}




